package com.yyq.sos.tag;

import java.io.Writer;

import org.apache.struts2.components.Component;

import com.opensymphony.xwork2.util.ValueStack;
import com.yyq.sos.util.page.PageForm;

public class Page extends Component {
	private String value;
	private PageForm pageForm;
	private boolean escape = false;
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public Page(ValueStack stack) {
		super(stack);
	}
	public boolean start(Writer writer) {
		boolean bool = super.start(writer);
		pageForm = (PageForm)stack.findValue(value);
		try{
			StringBuffer buf = new StringBuffer(200);
			buf.append("<table width=\"100%\"><tr>");
			
			buf.append("<td align=\"right\"><font color=\"#000000\" size=\"2\">")
			.append("<input id=\"pageForm.pageNo\" type=\"hidden\" name=\"pageForm.pageNo\" value=\"")
			.append(pageForm.getPageNo()).append("\" >")
			.append("共有:&nbsp;&nbsp;").append(pageForm.getTotalSize()).append("条记录/")
			.append(pageForm.getTotalPage()).append("页  ")			
			.append("当前第").append(pageForm.getPageNo()).append("页  ")
			.append("<a href=\"#\" onclick=\"javascript:refresh(")
			.append(pageForm.getPageNo()).append(");\"><font color=\"#000000\" size=\"2\">刷新</font></a>|");
			
			if(pageForm.isHasPrev()){
				buf.append("<a href=\"#\" onclick=\"javascript:goPage(");
				buf.append(pageForm.getPrevPage());
				buf.append(");\"><font color=\"#000000\" size=\"2\">上一页</font></a>|");
			}else
				buf.append("<font color=\"#000000\" size=\"2\">上一页</font>|");
			if(pageForm.isHasNext()){
				buf.append("<a href=\"#\" onclick=\"javascript:goPage(");
				buf.append(pageForm.getNextPage());
				buf.append(");\"><font color=\"#000000\" size=\"2\">下一页</font></a>|");
			}else
				buf.append("<font color=\"#000000\" size=\"2\">下一页</font>|");
			if(pageForm.isHasFirst())
				buf.append("<a href=\"#\" onclick=\"javascript:goPage(1);\"><font color=\"#000000\" size=\"2\">首页</font></a>|");
			else
				buf.append("<font color=\"#000000\" size=\"2\">首页</font>|");
			if(pageForm.isHasLast()){
				buf.append("<a href=\"#\" onclick=\"javascript:goPage(");
				buf.append(pageForm.getTotalPage());
				buf.append(");\"><font color=\"#000000\" size=\"2\">末页</font></a>");
			}
			else
				buf.append("<font color=\"#000000\" size=\"2\">末页</font>");
		
			buf.append("</font></td>")
			.append("</tr></table>");
			writer.write(buf.toString());
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return bool;
	}
	public void setEscape(boolean escape) {
		this.escape = escape;
	}
	public boolean isEscape() {
		return escape;
	}
}
